<?php 
//var_dump($this->rsNhanvien);
?>


<header class="panel-heading">
	<strong> Danh Sách khu vực </strong>
</header>
<div class="panel-body">
	<div class="adv-table editable-table ">
		<div class="clearfix">
			<div class="btn-group">
				<a class="btn btn-success btnadd" data-toggle="modal" href="#addModal">
					Thêm khu vực <i class="icon-plus"></i>
				</a>
			</div>
			
			<div class="btn-group pull-right">
				<button data-toggle="dropdown" class="btn dropdown-toggle">Xuất dữ liệu <i class="icon-angle-down"></i></button>
				<ul class="dropdown-menu pull-right">
					<li><a href="#">Xem in</a></li>
					<li><a href="#">Xuất PDF</a></li>
					<li><a href="#">Xuất Excel</a></li>
				</ul>
			</div>
		</div>
		<div class="space15"></div>
	  <div role="grid" class="dataTables_wrapper form-inline" id="editable-sample_wrapper">
		
		<table id="editable-sample" class="table table-striped table-hover table-bordered dataTable" aria-describedby="editable-sample_info">
			<thead>
				<tr role="row">
					<th class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 10px;" aria-label="Username">Stt</th>
					<th class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 168px;" aria-label="Username">Tên khu vực</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 90px;" aria-label="Delete: activate to sort column ascending">Trạng Thái</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 70px;" aria-label="Delete: activate to sort column ascending"></th>
				</tr>
		  </thead>
		  
		  <tbody role="alert" aria-live="polite" aria-relevant="all">
			  <?php 
			  $stt = 0;
			  foreach($this->rsKhuvuc as $rs) { 
				$stt++;
				?>
			  <tr>
				  <td class="  sorting_1"><?php echo $stt ?></td>
				  <td class="  sorting_1"><?php echo $rs['Ten'] ?></td>				  
				  <td style="text-align: center" class="center"> <a href=""><i class="icon-check-sign"></i></a> </td>
				  <td style="text-align: center">
				  	<a class="ograne  edit" data-toggle="modal" href="#addModal" idKhuvuc="<?php echo $rs['Id'] ?>"><i class="icon-edit"></i></a> | 
				  	<a class="red xoakhuvuc" href="javascript:;" class="delete" data="<?php echo $rs['Id']?>" ><i class="icon-remove"></i></a></td>
			  </tr>
			  <?php } ?>
		</tbody>
		</table>
  </div>
	</div>
</div>
        
		
                              
                              
							  
<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
	  <div class="modal-content">
		  <div class="modal-header">
			  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			  <h4 class="modal-title">Thêm khu vực</h4>
		  </div>
		  <div class="modal-body">
	
			  <form id="frmaddkhuvuc" class="form-horizontal tasi-form" method="get">
			  <input type="hidden" name="idKhuvuc" id="idKhuvuc" value="-1" />
				<div class="form-group" id="grpTen">
					<label class="col-sm-2 col-sm-2 control-label">Tên khu vực</label>
					<div class="col-sm-10">
						<input name="ten" id="ten" type="text" class="form-control" value = "">
					</div>
				</div>	
				
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Trạng thái</label>
					<div class="col-sm-10">
						<input name="trangthai" type="checkbox" checked="" data-toggle="switch" />
					</div>
				</div>
				
				
			  </form>

		  </div>
		  <div class="modal-footer">
			  <button data-dismiss="modal" class="btn btn-default" type="button">Đóng</button>
			  <button class="btn btn-success" type="button" id="luuthaydoi">Lưu thay đổi</button>
			  <button class="btn btn-success" type="button">Lưu & Thêm mới</button>
		  </div>
	  </div>
  </div>
</div>
<!-- modal -->



<!-- END JAVASCRIPTS -->
<script>
  jQuery(document).ready(function() {
	  //EditableTable.init();
	  

	$(".btnadd").click(function(){
		//$("#idKhuvuc").val("");
		//$("ten").val("");
	});

	  
	  jQuery("#luuthaydoi").click(function(){

		var values = $('#frmaddkhuvuc').serialize();
				
		  ten = $('#ten').val();
		  Id = $('#idKhuvuc').val();
		  
		  if(ten=="")
		  {
			  $('#grpTen').addClass("has-error");
			  alert("Vui lòng nhập tên khu vực");
			  $('#ten').focus();
			  return;
		  }		
		  
		  var saveUrl = "<?php echo _BASE_URL?>/quantri/khuvuc/themkhuvuc";
		  var formData = {ten: ten, Id: Id}; //Array 

		  var oTable = $('#editable-sample').dataTable();
		  
		  $.ajax({
			  url : saveUrl,
			  type: "POST",
			  data: values,
			  success: function(data, textStatus, jqXHR)
			  {
				  alert("Đã lưu thành công!");				 
				  location.href= "<?php echo _BASE_URL?>quantri/khuvuc";
				  $(".btn-default").click();
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Thất bại');
			  }
			});
	  });
	  
	  $(".xoakhuvuc").click(function(){

		  if (confirm("Bạn có muốn xóa dòng này không ?") == false) {
              return;
          }
          
		var deleteUrl = "<?php echo _BASE_URL?>/quantri/khuvuc/xoakhuvuc";
		var idKhuvuc = $(this).attr('data');
		var formData = {id: idKhuvuc}; //Array

		var tr = $(this).closest('tr');

		
		$.ajax({
			  url : deleteUrl,
			  type: "POST",
			  data : formData,
			  success: function(data, textStatus, jqXHR)
			  {
				  alert('Xóa thành công!');
				  deleteRowofTable(tr);
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Xóa thất bại');
			  }
		});
		
	  });

	  
	  function deleteRowofTable(tr)
	  {
		tr.css("background-color","#FF3700");
			tr.fadeOut(400, function(){
			tr.remove();
		});
	  }


	  ////////////////////////////////////////////////////////////////////
	  /// Sua Khu Vuc
	  $(".edit").click(function(){
		  var idKhuvuc = $(this).attr('idKhuvuc');
		  var editUrl = "<?php echo _BASE_URL?>quantri/khuvuc/sua/id/" + idKhuvuc;
		  
		  $.ajax({
			  url : editUrl,
			  type: "POST",
			  //data : formData,
			  dataType: "text",
			  
			  success: function(data, textStatus, jqXHR)
			  {
				  var Obkhuvuc = $.parseJSON(data);
				  $("#idKhuvuc").val(idKhuvuc);
				  $('#ten').val(Obkhuvuc[0].Ten);
				  $("#trangthai").val(Obkhuvuc[0].TrangThai);
				  
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Sửa thất bại');
			  }
			});
	  });	 
	  
  });
</script>